from hashlib import md5
from django.db.models import Q
from django.shortcuts import render,HttpResponse,redirect
from .models import G_Leave,S_Leave
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from .models import User
# Create your views here.

def index(request):
    print(request.session.get('role'))
    print(request.session.get('uname'))


    #登录才能访问
    if not request.session.get('role'):
        return HttpResponse('这是首页')
    else:
        return HttpResponse('访问失败，请先登录')

#添加学生假条
def add_leaver(request):

    if request.session.get('role') !=1:
        respone=HttpResponse('没有权限，拒绝访问',status=403,charset='utf-8')
        # respone['content_type']
        return respone


    if request.method=='GET':
        types=G_Leave.objects.all()
        return render(request,'add_leaver.html',{'types':types})
    else:
        stu_name=request.POST.get('stu_name')
        context=request.POST.get('context')
        createTime=request.POST.get('createTime')
        tid=request.POST.get('tid')

    S_Leave.objects.create(
        stu_name=stu_name,
        context=context,
        createTime=createTime,
        tid_id=tid

    )
    return HttpResponse('添加成功')

#老师
def show_leaver(request):
    if request.session.get('role')!=2:
        response=HttpResponse('没有权限，拒绝访问',status=403,charset='utf-8')
        response['content_type']='text/html'
        return response

    leaves_type=G_Leave.objects.all()
    status=request.GET.get('status','-1')


    if status == '-1':
        leaves = S_Leave.objects.all()
    else:
        leaves=S_Leave.objects.filter(tid_id=int(status)).all()
    status=int(status)


    page=request.GET.get('page',1)
    pageint=int(page)
    pagesize=3#每页展示两条数据
    paginator=Paginator(leaves,pagesize)
    pagex=paginator.page(pageint)#获取第x页的数据对象
    leaves=pagex.object_list#是一个queryset对象
    max=paginator.num_pages

    return render(request,'leaves.html',
                {'leaves':leaves,'leaves_type':leaves_type,'page':pageint,'status':status,'max':max})


def update_leaver(request):
    id=request.GET.get('id')
    status=request.GET.get('status')

    leave=S_Leave.objects.get(id=int(id))
    leave.status=int(status)
    leave.save()
    return redirect('go_leave:show_leaver')

def register(request):
    if request.method=='GET':
        return render(request,'register.html')
    else:
        uname=request.POST.get('uname','')
        upwd=request.POST.get('upwd','')
        upwd2=request.POST.get('upwd2','')
        role=request.POST.get('role','')

        if upwd == upwd2:#存数据，返回登录界面
            try:
                User.objects.get(uname=uname)
                return render(request, 'register.html',{'error':'用户名重复，请您换一个'})
            except:
                upwd=md5(upwd.encode(encoding='UTF-8')).hexdigest()#将明文换成乱码
                User.objects.create(
                    uname=uname,
                    upwd=upwd,
                    role=role

                )
                return HttpResponse('注册成功')
        else:#两次密码不一致
            return render(request, 'register.html',{'error':'两次密码不一样'})

    # return HttpResponse('----')



def loginx(request):
    if request.method == 'GET':
        return render(request,'login.html')
    else:
        uname=request.POST.get('uname')
        upwd=request.POST.get('upwd')
        upwd=md5(upwd.encode(encoding='UTF-8')).hexdigest()

        user=User.objects.filter(Q(uname=uname) & Q(upwd=upwd)).last()
        if user:
            request.session['uname']=user.uname
            request.session['role']=user.role


            if user.role == 1:#学生
                return redirect('go_leave:add_leaver')
            else:
                return redirect('go_leave:show_leaver')

            # return HttpResponse('登陆成功')
        else:
            return redirect('go_leave:loginx')
